<?php

class MyController extends BaseController
{

    public function init()
    {
        parent::init();
        $this->req = $this->getRequest();
    }

    /**
     * 我的竞猜
     */
    public function indexAction()
    {
        $req = array();
        $req['uid'] = $this->user_id;
        $rest = new \Base\Rest();
        $res = $rest->get('PlayList','', $req);
        if ($res->isSuccess()) {
            $playList = $res->getResponse();
        }else{
            $playList = array();
        }

        $req['week_type'] = $this->getWeekType();
        $res_total = $rest->get('MyRank','', $req);
        if ($res_total->isSuccess()) {
            $totalInfo = $res_total->getResponse();
        }else{
            $totalInfo = array(
                'total_sort'=>0,
                'total_score'=>0,
                'sort'=>0,
                'score'=>0,
            );
        }
        $this->view->totalInfo = $totalInfo;
        $this->view->playList = $playList;
        $this->view->nickname = $this->nickname;
        $this->view->headimgurl = $this->headimgurl;
        $this->view->title = '个人中心';
    }
    /*统计点击立即兑换点击次数*/
    public function countClickAction(){
        if (!$this->req->getPost()) {
            return "请求错误";
        }
        $postData = $this->req->getPost();
        $sendData = array(
            'type'=>$postData['type']??'',
        );
        $req = array();
        $req['user_id'] = $this->user_id;
        $rest = new \Base\Rest();
        $payload['payload'] = base64_encode(http_build_query($sendData));
        $res = $rest->Post('CountClick', $req, $payload);
        if($res->isSuccess()){
            return \Response::Success('','提交成功');
        }else{
            return \Response::Error('201',$res->getErrorMessage());
        }
    }
    /**
     * 我的奖品
     */
    public function winningAction(){
        $req = array();
        $req['uid'] = $this->user_id;
        $rest = new \Base\Rest();
        $res = $rest->get('MyWinning','', $req);
        if ($res->isSuccess()) {
            $winningList = $res->getResponse();
            $winningList = $winningList['list'];
        }else{
            $winningList = array();
        }

        $req['week_type'] = $this->getWeekType();
        $res_total = $rest->get('MyRank','', $req);
        if ($res_total->isSuccess()) {
            $totalInfo = $res_total->getResponse();
        }else{
            $totalInfo = array(
                'total_sort'=>0,
                'total_score'=>0,
                'sort'=>0,
                'score'=>0,
            );
        }
        $time_now = date('Y-m-d',time());
        $productList = array();
        $reqs['uid'] = $this->user_id;
        if($time_now >= '2018-06-21'){//第一周赛程结束，开始实物奖品派发
            $rest = new \Base\Rest();
            $res = $rest->get('MyProduct','', $reqs);

            if ($res->isSuccess()) {
                $productList = $res->getResponse();
            }else{
                $productList = array();
            }
        }
        $this->view->totalInfo = $totalInfo;
        $this->view->winningList = $winningList;
        $this->view->productList = $productList;
        $this->view->nickname = $this->nickname;
        $this->view->headimgurl = $this->headimgurl;
        $this->view->title = '个人中心';
    }

    public function getWinningAction(){
        $req['offset'] = isset($_GET['offset']) && $_GET['offset'] != '' ? $_GET['offset'] : 0;
        $req['size'] = isset($_GET['size']) && $_GET['size'] != '' ? $_GET['size'] : 0;
        $req['uid'] = $this->user_id;
        $rest = new \Base\Rest();
        $res = $rest->get('MyWinning','', $req);
        if ($res->isSuccess()) {
            $response = $res->getResponse();
            Response::Success($response);
        }else{
            Response::Error(201, $res->getErrorMessage());
        }
    }

    public function getAddressAction()
    {
        $req = array();
        $req['uid'] = $this->user_id;

        $rest = new \Base\Rest();
        $res = $rest->get('MyAddRess','', $req);
        if ($res->isSuccess()) {
            $addressInfo = $res->getResponse();
        }else{
            $addressInfo = array();
        }
        $this->view->addressInfo = $addressInfo;
        $result['htmlcode'] = $this->view->render('my/address.phtml');
        return Response::Success($result,'');
    }

    public function saveAddressAction()
    {
        $postData = $this->req->getPost();
        $sendData = array(
            'ship_username'=>$postData['user_name']??'',
            'ship_mobile'=>$postData['user_mobile']??'',
            'province_id'=>$postData['user_province']??'',
            'city_id'=>$postData['user_city']??'',
            'area_id'=>$postData['user_area']??'',
            'province'=>$postData['province_name']??'',
            'city'=>$postData['city_name']??'',
            'area'=>$postData['area_name']??'',
            'address' => $postData['user_address']??''
        );

        $req = array();
        $req['user_id'] = $this->user_id;
        $rest = new \Base\Rest();
        $payload['payload'] = base64_encode(http_build_query($sendData));
        $res = $rest->Post('MyAddRess', $req, $payload);
        if($res->isSuccess()){
            return \Response::Success('','提交成功');
        }else{
            return \Response::Error('201',$res->getErrorMessage());
        }
    }

    private function getWeekType()
    {
        $thisDate = date('Y-m-d');
        $week_type = 0;
        if($thisDate <= '2018-06-20'){
            $week_type = 1;
        }
        if('2018-06-21' <= $thisDate && $thisDate <= '2018-06-27'){
            $week_type = 2;
        }
        if('2018-06-28' <= $thisDate && $thisDate <= '2018-07-04'){
            $week_type = 3;
        }
        if('2018-07-06' <= $thisDate && $thisDate <= '2018-07-11'){
            $week_type = 4;
        }
        return $week_type;
    }

}